//
//  StatusOriginalView.swift
//  weibo
//
//  Created by 上官风雨 on 15/11/14.
//  Copyright © 2015年 上官风雨. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class StatusOriginalView: UIView {
    
    //定义一个约束的属性
    var bottomConstraints :Constraint?

    var statues :Status?{
        
        didSet{
            iconView.sd_setImageWithURL(statues?.user?.headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = statues?.user?.name
            vipView.image = statues?.user?.mbrankImage
            contentLabel.text = statues?.text
            userTypeView.image = statues?.user?.verified_type_Image
            
            self.bottomConstraints?.uninstall()
            if let imageurl = statues?.imageURLs where imageurl.count > 0{
                //有配图视图
                pictureView.imageURLs = statues?.imageURLs
                
                //更新配图视图的约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    pictureView.hidden = false
                    
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                
            }else{
                
                //更新配图视图的约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    pictureView.hidden = true
                    self.bottomConstraints = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
            
           
        
        }
        
    }
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置子控件
 private   func setupUI(){
        sepView.backgroundColor = UIColor(white: 0.6, alpha: 1.0)
        addSubview(sepView)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(vipView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(userTypeView)
        addSubview(pictureView)
    
        sepView.snp_makeConstraints { (make) -> Void in
            
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
            
    }
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.width.equalTo(StatusCellIconWitdth)
            make.height.equalTo(StatusCellIconWitdth)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
            make.top.equalTo(iconView.snp_top)
        }
        vipView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(iconView.snp_top)
        }
        userTypeView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
            
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconView)
            
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(contentLabel.snp_left)
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
//            make.width.equalTo(100)
//            make.height.equalTo(100)
            
        }
        self.snp_makeConstraints { (make) -> Void in
            
        self.bottomConstraints =   make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint

            
        }
        
    }
    
    lazy var sepView :UIView = UIView()
    
    //子控件头像的懒加载
    lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //昵称
   lazy var nameLabel :UILabel = UILabel(title: "张晓刚", size: 15, color: UIColor.darkGrayColor())
    //用户等级
   lazy var vipView :UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    //时间
   lazy var timeLabel :UILabel = UILabel(title: "11:11", size: 10, color: UIColor.orangeColor())
    //来源
    lazy var sourceLabel :UILabel = UILabel(title: "上官风雨", size: 10, color: UIColor.blackColor())
    
    //正文
    lazy var contentLabel :UILabel = UILabel(title: "为什么越来越多的人会单身?为什么越来越多的人会单身?为什么越来越多的人会单身?", size: 15, color: UIColor.lightGrayColor(),margin: StatusCellMargin)
    
    lazy var userTypeView: UIImageView = UIImageView()
    
    lazy var pictureView : StatusPictureView = StatusPictureView()
}
